package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.dao.ShortDao;
import cn.ztuo.bitrade.entity.QShort;
import cn.ztuo.bitrade.entity.Short;
import cn.ztuo.bitrade.util.PredicateUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShortService {

    @Autowired
    private ShortDao shortDao;


    public Short save(Short data){
        return shortDao.save(data);
    }


    public Short findOne(Long id){
        return shortDao.findByMemberId(id);
    }

    public Short findByMemberIdAndStatus(Long id,Long status){
        return shortDao.findByMemberIdAndStatus(id,status);
    }

    public List<Short> findAllByStatus(Long status){
        return shortDao.findAllByStatus(status);
    }

    public List<Short> findAllByStatusIn(List<Long> status){
        return shortDao.findAllByStatusIn(status);
    }
    /**
     * 获取空投列表
     * @param pageNum
     * @param pageSize
     * @param username
     * @param status
     * @return
     */
    public Page<Short> getList(Integer pageNum, Integer pageSize, String username, Long status){
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        if(StringUtils.isNotBlank(username)){
            booleanExpressions.add(QShort.short$.username.eq(username));
        }
        if(status==1 || status==2){
         booleanExpressions.add(QShort.short$.status.eq(status));
        }
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, sort);
        return shortDao.findAll(predicate,pageRequest);
    }
}
